package com.snopy.designPattern.observer;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author snopy
 * @version 1.0.0
 * @classname EventManager
 * @description
 * @email 77912204@qq.com
 * @date 2022/03/16 16:29
 */
public class EventManager {
    Map<String, List<EventListener>> listeners = new HashMap<>();

    public EventManager(String...operations) {
        for (String operation:operations){
            this.listeners.put(operation,new ArrayList<>());
        }
    }
    /**
     * @Description 订阅
     * @param eventType:
     * @param listener:
     * @return: void
     * @Date 2022-03-16
     * @Author txl77912204@gmail.com
     **/
    public void subscribe(String eventType,EventListener listener){
        this.listeners.get(eventType).add(listener);
    }
    /**
     * @Description 取消订阅
     * @param eventType:
     * @param listener:
     * @return: void
     * @Date 2022-03-16
     * @Author txl77912204@gmail.com
     **/
    public void unsubscribe(String eventType,EventListener listener){
        this.listeners.get(eventType).remove(listener);
    }
    public void notify(String eventType, File file){
        List<EventListener> listeners = this.listeners.get(eventType);
        for (EventListener listener:listeners) {
            listener.update(eventType,file);
        }
    }



}
